#ifndef __INVERTEDINDEX_H__
#define __INVERTEDINDEX_H__

#include <string>
#include <unordered_map>
#include <vector>
#include<map>
#include "WebPage.hh"
using std::string;
using std::unordered_map;
using std::pair;
using std::vector;
using std::map;

struct Offset{
    int id;
    int start;
    int len;
};

class InvertedIndex{
public:
    InvertedIndex(const string &offsetLibPath, const string &netLibFilePath, const string &invertedIndexFilePath, 
                                const string &ZhStopWordsFilePath, const string &EngStopWordsFilePath, 
                                const map<int,pair<int,int>> &offsetLib, const vector<WebPage> &pageLib);
    ~InvertedIndex();

    int readStopWords();

    int readOffsetFromFile();
    int readOffset();
    void filterStopWords(vector<string> &words);
    int generateTableFromFile();
    int generateTable();
    int buildInvertedIndexTable();

    int writerFile();

    //测试用函数
    void printOffsetVector();
    void printTable();
    void printInvertedIndexTable();
    void printPageLibId();
private:

    map<int,pair<int,int>> _offsetLib; 
    vector<WebPage> _pageLib;

    vector<string> _stopWords;
    vector<Offset> _offsetVector; //网页偏移表
    string _ZhStopWordsFilePath;
    string _EngStopWordsFilePath;
    string _offsetLibPath;
    string _netLibFilePath;
    string _invertedIndexFilePath;
    unordered_map<string, vector<pair<int, int>>> _table; //词频表<关键字, <文章ID, 关键字在这篇文章出现的次数>>
    unordered_map<string, vector<pair<int, double>>> _invertIndexTable; //倒排索引表
};

#endif 
